//
//  XMAlbumScanController.m
//  XiaoMiao
//
//  Created by 23 on 2018/1/29.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "XMAlbumScanController.h"
#import "XMAlbumScanCell.h"
#import <Photos/Photos.h>
#import "XMAlbumScanModel.h"

@interface XMAlbumScanController () <UICollectionViewDataSource, UICollectionViewDelegate>

/**collectionView*/
@property(nonatomic,weak) UICollectionView *collectionView;

@property(nonatomic,strong) NSMutableArray *images;

@end

@implementation XMAlbumScanController

static NSString * const Id = @"scan";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setupUI];
    
    [self loadData];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    
    self.collectionView.frame = self.view.bounds;
}



#pragma mark - UI
- (void)setupUI
{
    self.title = @"照片图库";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_camera_takephoto_blue"] style:UIBarButtonItemStyleDone target:self action:@selector(takePhoto)];
    self.navigationController.navigationBar.translucent = NO;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat wh = (kScreenWidth - 3) / 4;
    layout.itemSize = CGSizeMake(wh, wh);
    layout.minimumLineSpacing = 2;
    layout.minimumInteritemSpacing = 0;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass(XMAlbumScanCell.class) bundle:nil] forCellWithReuseIdentifier:Id];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
}


#pragma mark - action
- (void)takePhoto
{
    [self.navigationController popViewControllerAnimated:NO];
}

#pragma mark - UICollectionViewDelegate, UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XMAlbumScanCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:Id forIndexPath:indexPath];
    
    cell.imageModel = self.images[indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    XMAlbumScanModel *imageModel = self.images[indexPath.row];
    if (self.callback) {
        self.callback(imageModel);
    }
}


#pragma mark - 相册处理
- (void)loadData
{
    
    //获取相机胶卷所有图片
    PHFetchResult *assets = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
    //遍历
    WEAKSELF
    for (PHAsset *asset in assets) {
        
        XMAlbumScanModel *model = [[XMAlbumScanModel alloc] init];
        model.context = asset;
        
        //取出图片
        PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
        options.deliveryMode = PHImageRequestOptionsDeliveryModeOpportunistic;
        [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(200, 180) contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            
            model.image = result;
            [weakSelf.images addObject:model];
            
        }];
    }
    
    
    [self.collectionView reloadData];
    
}

#pragma mark - lazy

- (NSMutableArray *)images
{
    if (_images == nil) {
        _images = [NSMutableArray array];
    }
    return _images;
}


-(void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
    [[SDImageCache sharedImageCache]clearMemory];
    
}



@end
